<?php 


switch ($_POST['name']) {
	case 'username':
		checkUsername();
		break;
	case 'email':
		checkEmail();
		break;
	case 'phone':
		checkPhone();
		break;
	default:
		# code...
		break;
}

function checkUsername()
{
	$username = $_POST['data'];

	$preg = "/^[a-z][a-z0-9]{5,7}$/i";

	$c = preg_match($preg, $username);

	if(!$c){
		echo json_encode(['error'=>1,'info'=>'用户名不符合要求，6-8位']);exit;
	}

	$userlist = file_get_contents('./Db/user.txt');

	$userlist = json_decode($userlist,true);

	foreach ($userlist as  $user) {
		if($user['username']==$username){
			echo json_encode(['error'=>1,'info'=>'用户名已存在']);
			exit;
		}
	}

	echo json_encode(['error'=>0,'info'=>'用户名可用']);
			exit;

}

function checkEmail()
{
	$email = $_POST['data'];

	$preg = "/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/";

	$c = preg_match($preg, $email);

	if(!$c){
		echo json_encode(['error'=>1,'info'=>'邮箱格式不对']);exit;
	}

	$userlist = file_get_contents('./Db/user.txt');

	$userlist = json_decode($userlist,true);

	foreach ($userlist as  $user) {
		if($user['email']==$email){
			echo json_encode(['error'=>1,'info'=>'邮箱已使用']);
			exit;
		}
	}

	echo json_encode(['error'=>0,'info'=>'可用']);
			exit;

}

function checkPhone()
{
	$phone = $_POST['data'];

	$preg = "/^1[3|4|5|8][0-9]\d{4,8}$/";

	$c = preg_match($preg, $phone);

	if(!$c){
		echo json_encode(['error'=>1,'info'=>'手机格式不对']);exit;
	}

	$userlist = file_get_contents('./Db/user.txt');

	$userlist = json_decode($userlist,true);

	foreach ($userlist as  $user) {
		if($user['phone']==$phone){
			echo json_encode(['error'=>1,'info'=>'手机已使用']);
			exit;
		}
	}

	echo json_encode(['error'=>0,'info'=>'可用']);
			exit;

}











 ?>